/**
 * 
 */
package com.dycn.posdashboard.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dycn.posdashboard.dao.BaseDaoImpl;
import com.dycn.posdashboard.dao.PosTransactionDataDao;
import com.dycn.posdashboard.data.PosTransactionData;
import com.dycn.posdashboard.data.PosTransactionDataStats;
import com.dycn.posdashboard.data.StatsData001;
import com.dycn.posdashboard.data.StatsData002;
import com.dycn.posdashboard.data.StatsData003;
import com.dycn.posdashboard.data.StatsTimeUnit;
import com.dycn.posdashboard.dbhelper.DbContextHolder;

/**
 * @author gongqiyi
 *
 */
public class PosTransactionDataDaoImpl extends BaseDaoImpl implements
		PosTransactionDataDao {

	/* (non-Javadoc)
	 * @see com.dycn.posdashboard.dao.PosTransactionDataDao#getPosTransactionDataListBy(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.util.Date, java.util.Date, int, int)
	 */
	@Override
	public List<PosTransactionData> getPosTransactionDataListBy(
			String dbSource, String branchNo, String merchantNo, String cardNo,
			Date startTime, Date endTime, int startIndex, int endIndex) {
		// TODO Auto-generated method stub
		
    	log.debug("getPosTransactionDataListBy thread id:"+Thread.currentThread().getId());
        DbContextHolder.setDbSource(dbSource);
        log.debug("getPosTransactionDataListBy dbType=" + dbSource);
        Map<String,Object> paramaters=new HashMap<String,Object>();
        paramaters.put("branchNo", (branchNo==null||branchNo.trim().equals(""))?null:branchNo);
        paramaters.put("merchantNo", (merchantNo==null||merchantNo.trim().equals(""))?null:merchantNo);
        paramaters.put("cardNo", (cardNo==null||cardNo.trim().equals(""))?null:cardNo);
        paramaters.put("startTime", startTime);
        paramaters.put("endTime", endTime);
        
        if(startIndex>=1){
            paramaters.put("startIndex", startIndex-1);
            paramaters.put("maxRecordsPerPage", (endIndex-startIndex+1));
        }else{
        	 paramaters.put("startIndex", null);
        }

        
        return this.getSqlMapClientTemplate().queryForList("posTransactionDataListBy", paramaters);
        
	}
	
	@Override
	public int getTotalRecordsOfPosTransactionDataListBy(String dbSource,
			String branchNo, String merchantNo, String cardNo, Date startTime,
			Date endTime, int startIndex, int endIndex) {
		// TODO Auto-generated method stub
    	log.debug("getTotalRecordsOfPosTransactionDataListBy thread id:"+Thread.currentThread().getId());
        DbContextHolder.setDbSource(dbSource);
        log.debug("getTotalRecordsOfPosTransactionDataListBy dbType=" + dbSource);
        Map<String,Object> paramaters=new HashMap<String,Object>();
        paramaters.put("branchNo", (branchNo==null||branchNo.trim().equals(""))?null:branchNo);
        paramaters.put("merchantNo", (merchantNo==null||merchantNo.trim().equals(""))?null:merchantNo);
        paramaters.put("cardNo", (cardNo==null||cardNo.trim().equals(""))?null:cardNo);
        paramaters.put("startTime", startTime);
        paramaters.put("endTime", endTime);
        
        
        return (Integer)this.getSqlMapClientTemplate().queryForObject("totalRecordsOfPosTransactionDataListBy", paramaters);
	}
	
	
	@Override
	public int getTotalRecordsOfPosTransactionDataStatsByy(String dbSource,
			String branchNo, String merchantNo, String cardNo, Date startTime,
			Date endTime,StatsTimeUnit statsTimeUnit) {
		// TODO Auto-generated method stub
		
    	log.debug("getTotalRecordsOfPosTransactionDataStatsByy thread id:"+Thread.currentThread().getId());
        DbContextHolder.setDbSource(dbSource);
        log.debug("getTotalRecordsOfPosTransactionDataStatsByy dbType=" + dbSource);
        Map<String,Object> paramaters=new HashMap<String,Object>();
        paramaters.put("branchNo", (branchNo==null||branchNo.trim().equals(""))?null:branchNo);
        paramaters.put("merchantNo", (merchantNo==null||merchantNo.trim().equals(""))?null:merchantNo);
        paramaters.put("cardNo", (cardNo==null||cardNo.trim().equals(""))?null:cardNo);
        paramaters.put("startTime", startTime);
        paramaters.put("endTime", endTime);
        
        if(statsTimeUnit!=null) paramaters.put("timeGroupUnit", statsTimeUnit.toString());
        
        if((branchNo==null||branchNo.trim().equals(""))
        		&&(merchantNo==null||merchantNo.trim().equals(""))
        		&&(cardNo==null||cardNo.trim().equals(""))){
        	 paramaters.put("noGroup", new Object());
        }else{
        	paramaters.put("noGroup", null);
        }
        return (Integer)this.getSqlMapClientTemplate().queryForObject("totalRecordsOfPosTransactionDataStatsByy", paramaters);
	}

	public int getTotalRecordsOfPosTransactionDataStatsByyBranch(String dbSource,
			String branchNo, Date startTime,
			Date endTime) {
		// TODO Auto-generated method stub
		
    	log.debug("getTotalRecordsOfPosTransactionDataStatsByyBranch thread id:"+Thread.currentThread().getId());
        DbContextHolder.setDbSource(dbSource);
        log.debug("getTotalRecordsOfPosTransactionDataStatsByyBranch dbType=" + dbSource);
        Map<String,Object> paramaters=new HashMap<String,Object>();
        paramaters.put("branchNo", (branchNo==null||branchNo.trim().equals(""))?null:branchNo);
        paramaters.put("startTime", startTime);
        paramaters.put("endTime", endTime);
        
        //if(statsTimeUnit!=null) paramaters.put("timeGroupUnit", statsTimeUnit.toString());
        
//        if((branchNo==null||branchNo.trim().equals(""))
//        		&&(merchantNo==null||merchantNo.trim().equals(""))
//        		&&(cardNo==null||cardNo.trim().equals(""))){
//        	 paramaters.put("noGroup", new Object());
//        }else{
//        	paramaters.put("noGroup", null);
//        }
        return (Integer)this.getSqlMapClientTemplate().queryForObject("totalRecordsOfPosTransactionDataStatsByBranchNo", paramaters);
	}
	
	
	/* 
	 * 
	 *统计订单信息
	 * (non-Javadoc)
	 * @see com.dycn.posdashboard.dao.PosTransactionDataDao#getBranchPosTransactionDataStatsBy(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.util.Date, java.util.Date)
	 */
	@Override
	public List<PosTransactionDataStats> getPosTransactionDataStatsBy(
			String dbSource, String branchNo, String merchantNo, String cardNo,
			Date startTime, Date endTime,int startIndex,int endIndex,StatsTimeUnit statsTimeUnit) {
		// TODO Auto-generated method stub
    	log.debug("getPosTransactionDataStatsBy thread id:"+Thread.currentThread().getId());
        DbContextHolder.setDbSource(dbSource);
        log.debug("getPosTransactionDataStatsBy dbType=" + dbSource);
        Map<String,Object> paramaters=new HashMap<String,Object>();
        paramaters.put("branchNo", (branchNo==null||branchNo.trim().equals(""))?null:branchNo);
        paramaters.put("merchantNo", (merchantNo==null||merchantNo.trim().equals(""))?null:merchantNo);
        paramaters.put("cardNo", (cardNo==null||cardNo.trim().equals(""))?null:cardNo);
        paramaters.put("startTime", startTime);
        paramaters.put("endTime", endTime);
        
        if(statsTimeUnit!=null) paramaters.put("timeGroupUnit", statsTimeUnit.toString());
        
        if(startIndex>0){
            paramaters.put("startIndex", startIndex-1);
            paramaters.put("maxRecordsPerPage", (endIndex-startIndex+1));
        }

        
        return (List<PosTransactionDataStats>)this.getSqlMapClientTemplate().queryForList("posTransactionDataStatsBy", paramaters);
	}
	
	@Override
	public List<PosTransactionDataStats> getPosTransactionDataStatsByBranch(
			String dbSource, String branchNo, Date startTime, Date endTime,
			int startIndex, int endIndex) {
		// TODO Auto-generated method stub
    	log.debug("getPosTransactionDataStatsBy thread id:"+Thread.currentThread().getId());
        DbContextHolder.setDbSource(dbSource);
        log.debug("getPosTransactionDataStatsBy dbType=" + dbSource);
        Map<String,Object> paramaters=new HashMap<String,Object>();
        paramaters.put("branchNo", (branchNo==null||branchNo.trim().equals(""))?null:branchNo);

        paramaters.put("startTime", startTime);
        paramaters.put("endTime", endTime);
        

        if(startIndex>0){
            paramaters.put("startIndex", startIndex-1);
            paramaters.put("maxRecordsPerPage", (endIndex-startIndex+1));
        }

        return (List<PosTransactionDataStats>)this.getSqlMapClientTemplate().queryForList("PosTransactionDataStatsByBranchNo", paramaters);
        //return (List<PosTransactionDataStats>)this.getSqlMapClientTemplate().queryForList("PosTransactionDataStatsByBranchNo", paramaters);
	}
	
	

	@Override
	public PosTransactionData insertPosTransactionData(String dbSource,
			PosTransactionData posTransactionData) {
		// TODO Auto-generated method stub
		log.debug("insertPosTransactionData thread id:"+Thread.currentThread().getId());
        DbContextHolder.setDbSource(dbSource);
        log.debug("insertPosTransactionData dbType=" + dbSource);
        
        this.getSqlMapClientTemplate().insert("insertPosTransactionData", posTransactionData);
		
        return posTransactionData;
		
	}

	@Override
	public List<PosTransactionData> batchInsertPosTransactionData(
			String dbSource, List<PosTransactionData> posTransactionDataList) {
		// TODO Auto-generated method stub
		log.debug("batchInsertPosTransactionData thread id:"+Thread.currentThread().getId());
        DbContextHolder.setDbSource(dbSource);
        log.debug("batchInsertPosTransactionData dbType=" + dbSource);
        List<PosTransactionData> resultList=null;
        try {
			this.batchInsert("insertPosTransactionData", posTransactionDataList);
			resultList=posTransactionDataList;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			resultList=null;
			e.printStackTrace();
		}

        return resultList;
	}
	
	@Override
	public List<PosTransactionData> batchUpdatePosTransactionData(
			String dbSource, List<PosTransactionData> posTransactionDataList) {
		// TODO Auto-generated method stub
		log.debug("batchUpdatePosTransactionData thread id:"+Thread.currentThread().getId());
        DbContextHolder.setDbSource(dbSource);
        log.debug("batchUpdatePosTransactionData dbType=" + dbSource);
        
        try {
			this.batchUpdate("updatePosTransactionData", posTransactionDataList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			posTransactionDataList=null;
			e.printStackTrace();
		}

        return posTransactionDataList;
	}

	@Override
	public List<StatsData001> getStatsData001(
			String dbSource, String branchNo, String merchantNo, String cardNo,
			Date startTime, Date endTime) {
		// TODO Auto-generated method stub
    	log.debug("getPosTransactionDataStatsBy thread id:"+Thread.currentThread().getId());
        DbContextHolder.setDbSource(dbSource);
        log.debug("getPosTransactionDataStatsBy dbType=" + dbSource);
        Map<String,Object> paramaters=new HashMap<String,Object>();
        paramaters.put("branchNo", (branchNo==null||branchNo.trim().equals(""))?null:branchNo);
        paramaters.put("merchantNo", (merchantNo==null||merchantNo.trim().equals(""))?null:merchantNo);
        paramaters.put("cardNo", (cardNo==null||cardNo.trim().equals(""))?null:cardNo);
        paramaters.put("startTime", startTime);
        paramaters.put("endTime", endTime);

        
        return (List<StatsData001>)this.getSqlMapClientTemplate().queryForList("query_StatsData001", paramaters);
	}

	@Override
	public List<StatsData002> getStatsData002(String dbSource, String branchNo,
			String merchantNo, String cardNo, Date startTime, Date endTime) {
		// TODO Auto-generated method stub
    	log.debug("getStatsData002 thread id:"+Thread.currentThread().getId());
        DbContextHolder.setDbSource(dbSource);
        log.debug("getStatsData002 dbType=" + dbSource);
        Map<String,Object> paramaters=new HashMap<String,Object>();
        paramaters.put("branchNo", (branchNo==null||branchNo.trim().equals(""))?null:branchNo);
        paramaters.put("merchantNo", (merchantNo==null||merchantNo.trim().equals(""))?null:merchantNo);
        paramaters.put("cardNo", (cardNo==null||cardNo.trim().equals(""))?null:cardNo);
        paramaters.put("startTime", startTime);
        paramaters.put("endTime", endTime);

        
        return (List<StatsData002>)this.getSqlMapClientTemplate().queryForList("query_StatsData002", paramaters);
	}


	@Override
	public List<StatsData003> getStatsData003(String dbSource, String branchNo,
			String merchantNo, String cardNo, Date startTime, Date endTime) {
		// TODO Auto-generated method stub
    	log.debug("getStatsData002 thread id:"+Thread.currentThread().getId());
        DbContextHolder.setDbSource(dbSource);
        log.debug("getStatsData002 dbType=" + dbSource);
        Map<String,Object> paramaters=new HashMap<String,Object>();
        paramaters.put("branchNo", (branchNo==null||branchNo.trim().equals(""))?null:branchNo);
        paramaters.put("merchantNo", (merchantNo==null||merchantNo.trim().equals(""))?null:merchantNo);
        //paramaters.put("cardNo", (cardNo==null||cardNo.trim().equals(""))?null:cardNo);
        paramaters.put("startTime", startTime);
        paramaters.put("endTime", endTime);

        
        return (List<StatsData003>)this.getSqlMapClientTemplate().queryForList("query_StatsData003", paramaters);
	}

	@Override
	public List<StatsData001> getStatsData001Month(String dbSource,
			String branchNo, String merchantNo, String cardNo, Date startTime,
			Date endTime) {
		// TODO Auto-generated method stub
    	log.debug("getStatsData001Month thread id:"+Thread.currentThread().getId());
        DbContextHolder.setDbSource(dbSource);
        log.debug("getStatsData001Month dbType=" + dbSource);
        Map<String,Object> paramaters=new HashMap<String,Object>();
        paramaters.put("branchNo", (branchNo==null||branchNo.trim().equals(""))?null:branchNo);
        paramaters.put("merchantNo", (merchantNo==null||merchantNo.trim().equals(""))?null:merchantNo);
        paramaters.put("cardNo", (cardNo==null||cardNo.trim().equals(""))?null:cardNo);
        paramaters.put("startTime", startTime);
        paramaters.put("endTime", endTime);

        
        return (List<StatsData001>)this.getSqlMapClientTemplate().queryForList("query_StatsData001Month", paramaters);
	}

	@Override
	public List<StatsData001> getStatsData001Year(String dbSource,
			String branchNo, String merchantNo, String cardNo, Date startTime,
			Date endTime) {
		// TODO Auto-generated method stub
    	log.debug("getStatsData001Year thread id:"+Thread.currentThread().getId());
        DbContextHolder.setDbSource(dbSource);
        log.debug("getStatsData001Year dbType=" + dbSource);
        Map<String,Object> paramaters=new HashMap<String,Object>();
        paramaters.put("branchNo", (branchNo==null||branchNo.trim().equals(""))?null:branchNo);
        paramaters.put("merchantNo", (merchantNo==null||merchantNo.trim().equals(""))?null:merchantNo);
        paramaters.put("cardNo", (cardNo==null||cardNo.trim().equals(""))?null:cardNo);
        paramaters.put("startTime", startTime);
        paramaters.put("endTime", endTime);

        
        return (List<StatsData001>)this.getSqlMapClientTemplate().queryForList("query_StatsData001Year", paramaters);
	}

	@Override
	public List<PosTransactionDataStats> getMerchantPosTransactionDataStatsByBranch(
			String dbSource, String branchNo, Date startTime, Date endTime,
			int startIndex, int endIndex) {
		// TODO Auto-generated method stub
    	log.debug("getPosTransactionDataStatsBy thread id:"+Thread.currentThread().getId());
        DbContextHolder.setDbSource(dbSource);
        log.debug("getPosTransactionDataStatsBy dbType=" + dbSource);
        Map<String,Object> paramaters=new HashMap<String,Object>();
        paramaters.put("branchNo", (branchNo==null||branchNo.trim().equals(""))?null:branchNo);

        paramaters.put("startTime", startTime);
        paramaters.put("endTime", endTime);
        

        if(startIndex>0){
            paramaters.put("startIndex", startIndex-1);
            paramaters.put("maxRecordsPerPage", (endIndex-startIndex+1));
        }

        return (List<PosTransactionDataStats>)this.getSqlMapClientTemplate().queryForList("PosTransactionDataStatsByBranchNo2", paramaters);
	}

	@Override
	public int getTotalRecordsOfMerchantPosTransactionDataStatsByyBranch(
			String dbSource, String branchNo, Date startTime, Date endTime) {
		// TODO Auto-generated method stub
    	log.debug("getTotalRecordsOfPosTransactionDataStatsByyBranch thread id:"+Thread.currentThread().getId());
        DbContextHolder.setDbSource(dbSource);
        log.debug("getTotalRecordsOfPosTransactionDataStatsByyBranch dbType=" + dbSource);
        Map<String,Object> paramaters=new HashMap<String,Object>();
        paramaters.put("branchNo", (branchNo==null||branchNo.trim().equals(""))?null:branchNo);
        paramaters.put("startTime", startTime);
        paramaters.put("endTime", endTime);
        
        //if(statsTimeUnit!=null) paramaters.put("timeGroupUnit", statsTimeUnit.toString());
        
//        if((branchNo==null||branchNo.trim().equals(""))
//        		&&(merchantNo==null||merchantNo.trim().equals(""))
//        		&&(cardNo==null||cardNo.trim().equals(""))){
//        	 paramaters.put("noGroup", new Object());
//        }else{
//        	paramaters.put("noGroup", null);
//        }
        return (Integer)this.getSqlMapClientTemplate().queryForObject("totalRecordsOfPosTransactionDataStatsByBranchNo2", paramaters);
	}




}
